/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Dict;
import com.yuhanginfo.modules.sys.entity.Goods;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.entity.Vender;
import com.yuhanginfo.modules.sys.service.GoodsStoreUpDownService;
import com.yuhanginfo.modules.sys.service.VenderService;
import com.yuhanginfo.modules.sys.utils.DictUtils;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 门店商品上下架Controller
 * 
 * @author yinhao
 * @version 2017-10-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/goodsstoreupdown/")
public class GoodsStoreUpDownController extends BaseController {

	@Autowired
	private GoodsStoreUpDownService goodsStoreUpDownService;

	@Autowired
	private VenderService venderService;
	
	@ModelAttribute
	public Goods get(@RequestParam(required = false) String id) {
		Goods entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = goodsStoreUpDownService.get(id);
		}
		if (entity == null) {
			entity = new Goods();
		}
		return entity;
	}

	@RequiresPermissions("sys:goodsstoreupdown:view")
	@RequestMapping(value = { "list", "" })
	public String list(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(goods.getCreateBy() == null){
			User user = UserUtils.getUser();
			goods.setCreateBy(user);
		}
		Page<Goods> page = goodsStoreUpDownService.findPage(new Page<Goods>(request, response), goods);
		model.addAttribute("page", page);
		List<Dict> dictList = DictUtils.getDictList("goods_status");
		model.addAttribute("dictList",dictList);
		//制造商品牌
		List<Vender> venderList = venderService.findList(new Vender());
		model.addAttribute("venderList",venderList);
		return "modules/sys/goodsstoreupdown/goodsstoreupdownList";
	}

	@RequiresPermissions("sys:goodsstoreupdown:edit")
	@RequestMapping(value = "up")
	public String up(Goods goods,Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		goodsStoreUpDownService.up(goods);
		addMessage(redirectAttributes, "保存平台商品成功");
		return this.list(goods, request, response, model);
	}
	@RequiresPermissions("sys:goodsstoreupdown:edit")
	@RequestMapping(value = "down")
	public String down(Goods goods, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		goodsStoreUpDownService.down(goods);
		addMessage(redirectAttributes, "保存平台商品成功");
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsstoreupdown:edit")
	@RequestMapping(value = "batchUp")
	public String batchUp(Goods goods,String[] arrayId, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		goodsStoreUpDownService.batchUp(arrayId);
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsstoreupdown:edit")
	@RequestMapping(value = "batchDown")
	public String batchDown(Goods goods,String[] arrayId, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		goodsStoreUpDownService.batchDown(arrayId);
		return this.list(goods, request, response, model);
	}
}